(function($) {
    /**
     * 打印定位出错信息到控制台。
     * @param error {Object} 定位错误对象. 
     */
    var _errorToConsoleFun = function(error){
        switch (error.code) {
            case error.PERMISSION_DENIED:
                console.warn('User denied the request for Geolocation.');
                break;
            case error.POSITION_UNAVAILABLE:
                console.warn('Location information is unavailable.');
                break;
            case error.TIMEOUT:
                console.warn('The request to get user location timed out.');
                break;
            case error.UNKNOWN_ERROR:
                console.warn('An unknown error occurred.');
                break;
            default: 
                console.warn('Your browser does\'t support Geolocation.');
                break;
        }
    };    
    /**
     * 定位辅助类. 
     */
    ujs.define('geo', 'ujs.browser', {
        /**
         * 获取当前用户地理位置.
         * @param onSuccess(position) {Function}
         * @param onError(errorObj) {Function}
         * @param isAccurate {Boolean} 精确定位.
         */
        getCurrentPosition : function(onSuccess, onError, isAccurate) {
            var _this = this;            
            if(!Type.isFunction(onSuccess)){
                onSuccess = Function.empty;
            }
            if(!Type.isFunction(onError)){
                onError = Function.empty;
            }            
            var errorFun = function(err){
                _errorToConsoleFun(err);
                onError(err);
            };            
            //默认使用位置缓存模式.
            if(_this.position && isAccurate !== true){
                onSuccess(_this.position);                    
            } else {            
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(
                        function(position) {
                            onSuccess(_this.position = position);
                        }, 
                        errorFun, 
                        {
                            enableHighAccuracy : false,
                            timeout : 5000,
                            maximumAge : 0
                        }
                    );
                } else {
                    errorFun();
                }
            }
            return _this;
        }
    });
})($);